﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Stock.Core.MongoDB;
using System.Linq;
using System.Threading.Tasks;
using Stock.Model;
using Stock.Core.Common;
using Stock.Core.Services;
using Newtonsoft.Json;
using Stock.Model.Entities;
using Stock.Model.ViewModels;

namespace Stock.Core.Service
{
    public class stock1_m:BaseMongoEntity
    {

        public stock_1 data { get; set; }
        public long time { get; set; }
    }

	public class stock5_m : BaseMongoEntity
	{

		public stock_5 data { get; set; }
        public long time { get; set; }
	}

	public class stock15_m : BaseMongoEntity
	{

		public stock_15 data { get; set; }
        public long time { get; set; }
	}

	public class stock30_m : BaseMongoEntity
	{

		public stock_30 data { get; set; }
		public long time { get; set; }
	}

	public class stock60_m : BaseMongoEntity
	{
		public stock_60 data { get; set; }
		public long time { get; set; }
	}

	public class stock4h_m : BaseMongoEntity
	{
		public stock_4h data { get; set; }
		public long time { get; set; }
	}

	public class stock1d_m : BaseMongoEntity
	{
		public stock_1d data { get; set; }
		public long time { get; set; }
	}


	public class Stock1Service:IStock1Service
    {
        MongoBaseRepository<stock1_m> _mongo;
        MongoBaseRepository<stock_info_m> _info;
        private readonly IMongoService _s;
        private readonly IOrderService _o;
        private readonly IUnitOfWork _u;
        private readonly ILoggerService _l;
        private readonly IUserService _usr;

        public Stock1Service(IMongoService mongo, IUnitOfWork u, ILoggerService l)
		{
            _mongo = mongo.Reposityory<stock1_m>();
            _info = mongo.Reposityory<stock_info_m>();
            _s = mongo;
            _o = new OrderService(u);
            _u = u;
            _l = l;
            _usr = new UserService(u);
		}

        public async Task<stock1_m>  insert(stock_1 model)
		{
            model.id = Guid.NewGuid();
            stock1_m s = new stock1_m() { data = model };
			await _mongo.Create(s);
            var e = _mongo.GetQueryable().Where(a => a.data.id == s.data.id).SingleOrDefault();
            return e;
		}

        public async Task<double> todayHigh()
        {
            DateTime dt = DateTime.Now;
            long starttime = TimeHelper.ConvertDateTimeInt(new DateTime(dt.Year, dt.Month, dt.Day).AddHours(-8));
            double high = _s.Reposityory<stock5_m>().GetQueryable().Where(a => a.time > starttime).Max(a=>a.data.high);

            return high;
        }

		public async Task<double> todayLow()
		{
			DateTime dt = DateTime.Now;
            long starttime = TimeHelper.ConvertDateTimeInt(new DateTime(dt.Year, dt.Month, dt.Day).AddHours(-8));
            double Low = _s.Reposityory<stock5_m>().GetQueryable().Where(a => a.time > starttime).Min(a => a.data.low);

			return Low;
		}

        public async Task<BaseResult> getList(DateTime? starttime)
        {
			var qq = from l in _mongo.GetQueryable()
                                     select l;
			if (starttime != null)
                qq = qq.Where(aa => aa.data.date > starttime);
            var data = qq.OrderByDescending(q => q.data.date).Take(10000).ToList();
			return new BaseResult() { Data = data, DataCount = qq.Count(), IsSuccess = true };
        }

		public async Task<bool> count1(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();

            if(data.Count > 0)
            {
				// 赋值
				stock1_m s = new stock1_m();
				s.data = new stock_1();
				s.data.begin = data[0].data.sell;
				s.data.end = data[data.Count - 1].data.sell;
				s.data.high = data.Max(a => a.data.sell);
				s.data.low = data.Min(a => a.data.sell);
				s.data.date = dtstarttime;
				s.data.tag = 1;
				s.data.id = Guid.NewGuid();
				s.time = starttime;

				// 保存
				await _s.Reposityory<stock1_m>().Create(s);

                NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 1, data = s.data }));
            }
			

			return true;
		}

		public async Task<BaseResult> get1Today(long time)
		{
			var qq = from l in _s.Reposityory<stock1_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}

		public async Task<BaseResult> get1Last()
		{
			var qq = from l in _s.Reposityory<stock1_m>().GetQueryable()
					 select l;

            var data = qq.OrderByDescending(q => q.time).First();
			return new BaseResult() { Data = data, DataCount = 1, IsSuccess = true };
		}

		public int CheckExsit1(long Time)
		{
			var qq = from l in _s.Reposityory<stock1_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}

        public async Task<bool> insert1(K5ViewData data)
        {
			//判断是否存在
			var ExsitCount = CheckExsit1(data.date);
			if (ExsitCount > 0)
				return false;
            
			var kdate = TimeHelper.GetTime(data.date.ToString());

            kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
			stock_1 model = new stock_1()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock1_m s = new stock1_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock1_m>().Create(s);
            NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 1, isnew = 1, data=data }));

			return true;
        }

		public int CheckExsit15(long Time)
		{
			var qq = from l in _s.Reposityory<stock15_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}

		public async Task<bool> Insert15(K5ViewData data)
		{
			//判断是否存在
			var ExsitCount = CheckExsit15(data.date);
			if (ExsitCount > 0)
				return false;


			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
			stock_15 model = new stock_15()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock15_m s = new stock15_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock15_m>().Create(s);
			NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 15, isnew = 1, data = data }));

			return true;
		}

		public int CheckExsit30(long Time)
		{
			var qq = from l in _s.Reposityory<stock30_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}

		public async Task<bool> Insert30(K5ViewData data)
		{
			//判断是否存在
			var ExsitCount = CheckExsit30(data.date);
			if (ExsitCount > 0)
				return false;


			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
			stock_30 model = new stock_30()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock30_m s = new stock30_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock30_m>().Create(s);
			NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 30, isnew = 1, data = data }));

			return true;
		}

		public int CheckExsit60(long Time)
		{
            var qq = from l in _s.Reposityory<stock60_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}

		public async Task<bool> Insert60(K5ViewData data)
		{
			//判断是否存在
			var ExsitCount = CheckExsit60(data.date);
			if (ExsitCount > 0)
				return false;


			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
			stock_60 model = new stock_60()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock60_m s = new stock60_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock60_m>().Create(s);
			NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 60, isnew = 1, data = data }));

			return true;
		}

		public int CheckExsit4h(long Time)
		{
            var qq = from l in _s.Reposityory<stock4h_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}

		public async Task<bool> Insert4h(K5ViewData data)
		{
			//判断是否存在
			var ExsitCount = CheckExsit4h(data.date);
			if (ExsitCount > 0)
				return false;
            
			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
            stock_4h model = new stock_4h()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock4h_m s = new stock4h_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock4h_m>().Create(s);
			NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 240, isnew = 1, data = data }));

			return true;
		}

		public int CheckExsit1d(long Time)
		{
            var qq = from l in _s.Reposityory<stock1d_m>().GetQueryable()
					 where l.time == Time
					 select l;

			return qq.Count();
		}


		public async Task<bool> Insert1d(K5ViewData data)
		{

			//判断是否存在
			var ExsitCount = CheckExsit1d(data.date);
			if (ExsitCount > 0)
				return false;
            
			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);
            data.dtdate = kdate;
			stock_1d model = new stock_1d()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			stock1d_m s = new stock1d_m();
			s.data = model;
			s.time = data.date;

			// 保存
			await _s.Reposityory<stock1d_m>().Create(s);
			NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 250, isnew = 1, data = data }));

			return true;
		}

        public int CheckExsit(long Time)
        {
			var qq = from l in _s.Reposityory<stock5_m>().GetQueryable()
                                        where l.time == Time
					 select l;

            return qq.Count();
        }

        public async Task<bool> insert5( K5ViewData data)
        {
             //判断是否存在
            var ExsitCount = CheckExsit(data.date);
            if (ExsitCount > 0)
                return false;

			var kdate = TimeHelper.GetTime(data.date.ToString());

			kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);

			data.dtdate = kdate;

			stock_5 model = new stock_5()
			{
				date = kdate,
				begin = data.begin,
				end = data.end,
				high = data.high,
				low = data.low
			};

			//NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 5, data = s.data }));
            long start = TimeHelper.ConvertDateTimeInt(model.date.AddMinutes(-10));
            long end = TimeHelper.ConvertDateTimeInt(model.date.AddMinutes(-5));

            // getsettingend

            var logquery = from l in _u.Context.endLog
                           where l.date == kdate
                           orderby l.create_date descending
                           select l;
            var logdata=logquery.FirstOrDefault();

            if(logdata != null && logdata.id !=Guid.Empty)
            {
                model.end = logdata.value;
                model.tag = 1;

                data.end = logdata.value;
            }
            stock5_m s = new stock5_m();
            s.data = model;
            s.time = data.date;
			// 保存
			await _s.Reposityory<stock5_m>().Create(s);

            NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 5, data = data, isnew=1 }));

            //NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 5, isnew = 0 }));

			// 取上个节点的订单
			await CountOrder(start, end, model.begin, model.end);

            return true;
        }

		/// <summary>
		/// 计算上一个5分钟节点
		/// </summary>
		/// <returns>The count5.</returns>
		/// <param name="endtime">Endtime.</param>
        public async Task<bool> count5(long starttime, long endtime, DateTime dtstarttime)
		{
            _l.Log(new LogModel() { ExecuteStartTime = DateTime.Now, Message = $"count5({starttime}, {endtime})" }, "count5");

            // 判断是否存在
			var query = from l in _info.GetQueryable()
                                        where l.time == endtime
					 select l;
            var item5 =  query.Count();

            if(item5 > 0)
            {
                return false;
            }


			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();

            if (data.Count > 0)
            {
                // 赋值
                stock5_m s = new stock5_m();
                s.data = new stock_5();
                s.data.begin = data[0].data.sell;
                s.data.end = data[data.Count - 1].data.sell;
                s.data.high = data.Max(a => a.data.sell);
                s.data.low = data.Min(a => a.data.sell);
                s.data.date = dtstarttime;
                s.data.tag = 1;
                s.data.id = Guid.NewGuid();
                s.time = starttime;
                // 保存
                await _s.Reposityory<stock5_m>().Create(s);

                DateTime st = TimeHelper.GetTime(starttime.ToString());
                endtime = starttime;

                starttime = TimeHelper.ConvertDateTimeInt(st.AddMinutes(-5)) ;

                NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 5, data = s.data }));

                // 取上个节点的订单
                await CountOrder(starttime, endtime, s.data.begin, s.data.end);

            }
			return true;
		}
        public async Task CountOrder(long starttime, long endtime, double begin, double end)
        {
			List<order> orders = await _o.getUnCountOrders(starttime, endtime);

            _l.Log(new LogModel() { ExecuteStartTime = DateTime.Now, Message = $"getUnCountOrders({starttime}, {endtime})" }, "CountOrders");


			orders.ForEach(item =>
			{
				var u = _usr.getUser(item.userid).Result;


                if (end > begin)
                {
                	if (item.type == 1)
                		item.success = 1;
                	else
                		item.success = 2;
                }
                else if (end < begin)
                {
                	if (item.type == 1)
                		item.success = 2;
                	else
                		item.success = 1;
                }
                else
                {
                	item.success = 3;
                }

				item.status = 1;
                if (item.success == 2)
                    item.commission = 0;
                
                item.start_price = begin;
                item.end_price = end;

				var tt = _o.Update(item, false).Result;

				if (item.success == 1)
				{
					u.balance += item.moneny * 2 - item.commission;
					u.freeze -= item.moneny;
				}
				else if (item.success == 2)
				{
					u.freeze -= item.moneny;
				}
				else
				{
					u.balance += item.moneny;
					u.freeze -= item.moneny;
				}

				var t = _usr.UpdateUser(u, false).Result;

				_u.Commit();
                //_u.Context.Set<user>().AsNoTracking<user>();
			});

            NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 1000}));
        }

		public async Task<BaseResult> get5Last()
		{
			var qq = from l in _s.Reposityory<stock5_m>().GetQueryable()
					 select l;

            var data = qq.OrderByDescending(q => q.time).First();
			return new BaseResult() { Data = data, DataCount = 1, IsSuccess = true };
		}

		public async Task<BaseResult> get5Today(long time)
		{
            var qq = from l in _s.Reposityory<stock5_m>().GetQueryable()
                     where l.time >= time
                     select l;


            var data = qq.OrderByDescending(q => q.time).ToList();
            data.Sort((a, b) => a.time.CompareTo(b.time));

            var next = data[data.Count() - 1].data.date.AddMinutes(5);

            return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true,  };
		}

		public async Task<bool> count15(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();
            if (data.Count > 0)
            {
                // 赋值
                stock15_m s = new stock15_m();
                s.data = new stock_15();
                s.data.begin = data[0].data.sell;
                s.data.end = data[data.Count - 1].data.sell;
                s.data.high = data.Max(a => a.data.sell);
                s.data.low = data.Min(a => a.data.sell);
                s.data.date = dtstarttime;
                s.data.tag = 1;
                s.data.id = Guid.NewGuid();
                s.time = starttime;
                // 保存
                await _s.Reposityory<stock15_m>().Create(s);

                NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 15, data = s.data }));
            }

			return true;
		}

		public async Task<BaseResult> get15Today(long time)
		{
			var qq = from l in _s.Reposityory<stock15_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}

		public async Task<BaseResult> get15Last()
		{
			var qq = from l in _s.Reposityory<stock15_m>().GetQueryable()
					 select l;

            var data = qq.OrderByDescending(q => q.time).FirstOrDefault();
			return new BaseResult() { Data = data, DataCount = 1, IsSuccess = true };
		}


		public async Task<bool> count30(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();
			if (data.Count > 0)
			{
				
                // 赋值
				stock30_m s = new stock30_m();
				s.data = new stock_30();
				s.data.begin = data[0].data.sell;
				s.data.end = data[data.Count - 1].data.sell;
				s.data.high = data.Max(a => a.data.sell);
				s.data.low = data.Min(a => a.data.sell);
				s.data.date = dtstarttime;
				s.data.tag = 1;
				s.data.id = Guid.NewGuid();
				s.time = starttime;
				// 保存
				await _s.Reposityory<stock30_m>().Create(s);

				NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 30, data = s.data }));
			}

			return true;
		}

		public async Task<BaseResult> get30Today(long time)
		{
			var qq = from l in _s.Reposityory<stock30_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}

		public async Task<bool> count60(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();
			if (data.Count > 0)
			{
				// 赋值
				stock60_m s = new stock60_m();
				s.data = new stock_60();
				s.data.begin = data[0].data.sell;
				s.data.end = data[data.Count - 1].data.sell;
				s.data.high = data.Max(a => a.data.sell);
				s.data.low = data.Min(a => a.data.sell);
				s.data.date = dtstarttime;
				s.data.tag = 1;
				s.data.id = Guid.NewGuid();
				s.time = starttime;
				// 保存
				await _s.Reposityory<stock60_m>().Create(s);

				NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 60, data = s.data }));
			}

			return true;
		}

		public async Task<BaseResult> get60Today(long time)
		{
			var qq = from l in _s.Reposityory<stock60_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}


		public async Task<bool> count4h(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();
			if (data.Count > 0)
			{
				// 赋值
				stock4h_m s = new stock4h_m();
				s.data = new stock_4h();
				s.data.begin = data[0].data.sell;
				s.data.end = data[data.Count - 1].data.sell;
				s.data.high = data.Max(a => a.data.sell);
				s.data.low = data.Min(a => a.data.sell);
				s.data.date = dtstarttime;
				s.data.tag = 1;
				s.data.id = Guid.NewGuid();
				s.time = starttime;
				// 保存
				await _s.Reposityory<stock4h_m>().Create(s);

				NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 240, data = s.data }));
			}

			return true;
		}

		public async Task<BaseResult> get4hToday(long time)
		{
			var qq = from l in _s.Reposityory<stock4h_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}

		public async Task<bool> count1d(long starttime, long endtime, DateTime dtstarttime)
		{
			// 跟据时间段查找数据并排序
			var qq = from l in _info.GetQueryable()
					 where l.time >= starttime && l.time < endtime
					 select l;

			var data = qq.OrderBy(q => q.time).ToList();
			if (data.Count > 0)
			{
				// 赋值
				stock1d_m s = new stock1d_m();
				s.data = new stock_1d();
				s.data.begin = data[0].data.sell;
				s.data.end = data[data.Count - 1].data.sell;
				s.data.high = data.Max(a => a.data.sell);
				s.data.low = data.Min(a => a.data.sell);
				s.data.date = dtstarttime;
				s.data.tag = 1;
				s.data.id = Guid.NewGuid();
				s.time = starttime;
				// 保存
				await _s.Reposityory<stock1d_m>().Create(s);

				NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 24, data = s.data }));
			}

			return true;
		}

		public async Task<BaseResult> get1dToday(long time)
		{
			var qq = from l in _s.Reposityory<stock1d_m>().GetQueryable()
					 where l.time >= time
					 select l;


			var data = qq.OrderByDescending(q => q.time).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, DataCount = data.Count, IsSuccess = true };
		}

        // 获取指定时间的第一个数据
        public async Task<BaseResult> getfirst(long time)
        {
			var qq = from l in _s.Reposityory<stock_info_m>().GetQueryable()
					 where l.time >= time
					 select l;


            var data = qq.OrderBy(q => q.time).First();
            
			return new BaseResult() { Data = data, DataCount = 1, IsSuccess = true };
        }

        public async Task<BaseResult> get1Top()
        {
			var qq = from l in _s.Reposityory<stock1_m>().GetQueryable()
                                 orderby l.time descending 
					 select l;
            var data =qq.Take(300).ToList();
            data.Sort((a, b) => a.time.CompareTo(b.time));
            return new BaseResult() { Data = data, IsSuccess = false };

        }

		public async Task<BaseResult> get15Top()
		{
			var qq = from l in _s.Reposityory<stock15_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(300).ToList();
            data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

		public async Task<BaseResult> get5Top()
		{
			var qq = from l in _s.Reposityory<stock5_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(300).ToList();
            data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}


		public async Task<BaseResult> get51000()
		{
			var qq = from l in _s.Reposityory<stock5_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(500).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

        public async Task<BaseResult> inserts5(List<stock5_m> list)
		{
            var _mongo5 = _s.Reposityory<stock5_m>();
            await _mongo5.CreateMany(list);
            return new BaseResult() { IsSuccess = true };
		}


		public async Task<BaseResult> get151000()
		{
			var qq = from l in _s.Reposityory<stock15_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(500).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

		public async Task<BaseResult> inserts15(List<stock15_m> list)
		{
			var _mongo5 = _s.Reposityory<stock15_m>();
			await _mongo5.CreateMany(list);
			return new BaseResult() { IsSuccess = true };
		}

		public async Task<BaseResult> get11000()
		{
			var qq = from l in _s.Reposityory<stock1_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(500).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

		public async Task<BaseResult> inserts1(List<stock1_m> list)
		{
			var _mongo5 = _s.Reposityory<stock1_m>();
			await _mongo5.CreateMany(list);
			return new BaseResult() { IsSuccess = true };
		}

		public async Task<BaseResult> get301000()
		{
			var qq = from l in _s.Reposityory<stock30_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(500).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

		public async Task<BaseResult> inserts30(List<stock30_m> list)
		{
			var _mongo5 = _s.Reposityory<stock30_m>();
			await _mongo5.CreateMany(list);
			return new BaseResult() { IsSuccess = true };
		}

		public async Task<BaseResult> get601000()
		{
			var qq = from l in _s.Reposityory<stock60_m>().GetQueryable()
					 orderby l.time descending
					 select l;
			var data = qq.Take(500).ToList();
			data.Sort((a, b) => a.time.CompareTo(b.time));
			return new BaseResult() { Data = data, IsSuccess = false };

		}

		public async Task<BaseResult> inserts60(List<stock60_m> list)
		{
			var _mongo5 = _s.Reposityory<stock60_m>();
			await _mongo5.CreateMany(list);
			return new BaseResult() { IsSuccess = true };
		}



	}
}
